//
//  UIScrollView+LYAdd.m
//  LYDevelopKit
//
//  Created by 李阳 on 2018/5/26.
//

#import "UIScrollView+LYAdd.h"
#import "LYUIMacros.h"

LYSYNTH_DUMMY_CLASS(UIScrollView_LYAdd)

static BOOL _respondsToAdjustedContentInset;

@implementation UIScrollView (LYAdd)

+ (void)initialize
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _respondsToAdjustedContentInset = [self instancesRespondToSelector:@selector(adjustedContentInset)];
    });
}

- (UIEdgeInsets)inset
{
    if (@available(iOS 11.0, *))
    {
        if (_respondsToAdjustedContentInset)
        {
            return self.adjustedContentInset;
        }
    }
    
    return self.contentInset;
}

- (CGFloat)insetT
{
    return self.inset.top;
}
- (void)setInsetT:(CGFloat)insetT
{
    UIEdgeInsets inset = self.contentInset;
    inset.top = insetT;
    if (@available(iOS 11.0, *))
    {
        if (_respondsToAdjustedContentInset)
        {
            inset.top -= (self.adjustedContentInset.top - self.contentInset.top);
        }
    }
    self.contentInset = inset;
}
- (CGFloat)insetB
{
    return self.inset.bottom;
}
- (void)setInsetB:(CGFloat)insetB
{
    UIEdgeInsets inset = self.contentInset;
    inset.bottom = insetB;
    if (@available(iOS 11.0, *))
    {
        if (_respondsToAdjustedContentInset)
        {
            inset.bottom -= (self.adjustedContentInset.bottom - self.contentInset.bottom);
        }
    }
    self.contentInset = inset;
}
- (CGFloat)insetL
{
    return self.inset.left;
}
- (void)setInsetL:(CGFloat)insetL
{
    UIEdgeInsets inset = self.contentInset;
    inset.left = insetL;
    if (@available(iOS 11.0, *))
    {
        if (_respondsToAdjustedContentInset)
        {
            inset.left -= (self.adjustedContentInset.left - self.contentInset.left);
        }
    }
    self.contentInset = inset;
}
- (CGFloat)insetR
{
    return self.inset.right;
}
- (void)setInsetR:(CGFloat)insetR
{
    UIEdgeInsets inset = self.contentInset;
    inset.right = insetR;
    if (@available(iOS 11.0, *))
    {
        if (_respondsToAdjustedContentInset)
        {
            inset.right -= (self.adjustedContentInset.right - self.contentInset.right);
        }
    }
    self.contentInset = inset;
}

#pragma mark - contentOffset
- (CGFloat)offsetT
{
    return self.contentOffset.y;
}
- (void)setOffsetT:(CGFloat)offsetT
{
    CGPoint offset = self.contentOffset;
    offset.y = offsetT;
    [self setContentOffset:offset animated:YES];
}
- (CGFloat)offsetB
{
    return self.offsetT + self.bounds.size.height;
}
- (void)setOffsetB:(CGFloat)offsetB
{
    self.offsetT = offsetB - self.bounds.size.height;
}
- (CGFloat)offsetL
{
    return self.contentOffset.x;
}
- (void)setOffsetL:(CGFloat)offsetL
{
    CGPoint offset = self.contentOffset;
    offset.x = offsetL;
    [self setContentOffset:offset animated:YES];
}
- (CGFloat)offsetR
{
    return self.offsetL + self.bounds.size.width;
}
- (void)setOffsetR:(CGFloat)offsetR
{
    self.offsetL = offsetR - self.bounds.size.width;
}


#pragma mark - contentSize
- (CGFloat)contentW
{
    return self.contentSize.width;
}
- (void)setContentW:(CGFloat)contentW
{
    CGSize size = self.contentSize;
    size.width = contentW;
    self.contentSize = size;
}
- (CGFloat)contentH
{
    return self.contentSize.height;
}
- (void)setContentH:(CGFloat)contentH
{
    CGSize size = self.contentSize;
    size.height = contentH;
    self.contentSize = size;
}

/// 可见宽度
- (CGFloat)visibleW
{
    return self.contentW + self.insetL + self.insetR;
}

/// 可见高度
- (CGFloat)visibleH
{
    return self.contentH + self.insetT + self.insetB;
}

/// 是否在最顶端
- (BOOL)atTopPosition
{
    return self.offsetT == -self.insetT;
}
/// 是否在最底部
- (BOOL)atBottomPosition
{
    return self.offsetB == self.contentH + self.insetB;
}
/// 是否在最左边
- (BOOL)atLeftPosition
{
    return self.offsetL == -self.insetL;
}

/// 是否在最右边
- (BOOL)atRightPosition
{
    return self.offsetR == self.contentW + self.insetR;
}

/// 水平滚动一段距离
- (void)horizontalRollingWithDistance:(CGFloat)distance animated:(BOOL)animated
{
    CGPoint offset = self.contentOffset;
    offset.x += distance;
    [self setContentOffset:offset animated:animated];
}
/// 竖直滚动一段距离
- (void)verticalRollingWithDistance:(CGFloat)distance animated:(BOOL)animated
{
    CGPoint offset = self.contentOffset;
    offset.y += distance;
    [self setContentOffset:offset animated:animated];
}

- (NSUInteger)verticalPageIndex
{
    CGFloat height = self.bounds.size.height;
    return (self.offsetT + height * 0.5) / height;
}
- (NSUInteger)horizontalPageIndex
{
    CGFloat width = self.bounds.size.width;
    return (self.offsetL + width * 0.5) / width;
}
/// 水平滚动到第几页
- (void)horizontalRollingToPageIndex:(NSUInteger)index animated:(BOOL)animated
{
    CGFloat width = self.bounds.size.width;
    [self setContentOffset:CGPointMake(0.0f, width * index) animated:animated];
}
/// 竖直滚动到第几页
- (void)verticalRollingToPageIndex:(NSUInteger)index animated:(BOOL)animated
{
    CGFloat height = self.bounds.size.height;
    [self setContentOffset:CGPointMake(0.0f, height * index) animated:animated];
}

- (LYScrollDirection)scrollDirection
{
    LYScrollDirection direction = LYScrollDirectionUnknown;
    CGPoint point = [self.panGestureRecognizer translationInView:self.superview];
    if (point.y > 0.0f)
    {
        direction |= LYScrollDirectionUp;
    }
    else if (point.y < 0.0f)
    {
        direction |= LYScrollDirectionDown;
    }
    else if (point.x < 0.0f)
    {
        direction |= LYScrollDirectionLeft;
    }
    else if (point.x > 0.0f)
    {
        direction |= LYScrollDirectionRight;
    }
    
    return direction;
}

@end


